package com.myway.service.impl;

import com.alibaba.fastjson.JSON;
import com.myway.core.util.HttpClientUtil;
import com.myway.dao.*;
import com.myway.dto.ibe_param.FlightParam;
import com.myway.dto.order.domain.IntegralStatus;
import com.myway.pojo.*;
import com.myway.service.IntegralService;
import com.myway.service.OrderService;
import com.myway.service.TabMembIntegralService;
import com.myway.service.TicketService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Zap on 2018-10-10.
 */
public class IntegralServiceImpl implements IntegralService {

    @Autowired
    private FlyOrderMapper flyOrderMapper;

    @Autowired
    private FlySegmentMapper flySegmentMapper;

    @Autowired
    private TabMembIntegralService tabMembIntegralService;

    /*@Autowired
    private AVCommand avCommand;*/

    /**
     * 里程转换积分并入库
     */
    public void distanceToIntegral(TabMembIntegral tabMembIntegral, String orderId){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        FlyOrder flyOrder = flyOrderMapper.selectByPrimaryKey(Long.valueOf(orderId));
        Date now = new Date();
        List<FlySegment> flySegments = flySegmentMapper.getFlySegmentListByOrderId(flyOrder.getId());
        FlySegment flySegment = flySegments.get(0);
        tabMembIntegral.setOrderId(orderId);
        tabMembIntegral.setOrderNo(flyOrder.getOrderId());
        tabMembIntegral.setMoney(flyOrder.getTotalPrice());
        tabMembIntegral.setFlightDate(flySegment.getDepTime());
        tabMembIntegral.setAirlineCode(flySegment.getFlightNo().substring(0,2));
        tabMembIntegral.setFlightNum(flySegment.getFlightNo());
        tabMembIntegral.setCarbineClass(flySegment.getCabinClass());
        tabMembIntegral.setStartCode(flySegment.getDepCode());
        tabMembIntegral.setArriveCode(flySegment.getArrCode());
        tabMembIntegral.setIntegralStatus(IntegralStatus.NORMAL.getStatus());
        tabMembIntegral.setDisposeTime(now);
        tabMembIntegral.setIntegralTime(now);
        TabIntegralRecord tabIntegralRecord = new TabIntegralRecord();
        tabIntegralRecord.setOrderId(orderId);
        tabIntegralRecord.setCreateTime(now);
        double distance = 0;
        for (FlySegment segment:flySegments) {
            FlightParam flightParam = new FlightParam();
            flightParam.setFlightNo(flySegment.getFlightNo());
            flightParam.setFlightDate(sdf.format(flySegment.getDepTime()));
            String flyDistance = HttpClientUtil.doPostJson("http://47.75.92.162:49001/av/flyDistance", JSON.toJSONString(flightParam));
            //String flyDistance = avCommand.getFlyDistance(flightParam.getFlightNo(), DateUtils.StringToDate(flightParam.getFlightDate(),"yyyy-MM-dd"));
            if(StringUtils.isBlank(flyDistance) || (Double.valueOf(flyDistance).doubleValue())<=0)
                flyDistance = segment.getDistance();
            distance += Double.valueOf(flyDistance).doubleValue();
        }
        tabMembIntegral.setIntegralNum(distance);
        tabIntegralRecord.setAddIntegral(distance);
        tabIntegralRecord.setReduceIntegral(0d);
        tabIntegralRecord.setModifyReason("BOOKING");
        tabIntegralRecord.setStatus("NORMAL");
        tabMembIntegralService.addMembIntegral(tabMembIntegral,tabIntegralRecord);
        /*List<FlySegment> flySegments = data.getFlySegments();
        for (FlySegment flySegment:flySegments) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            FlightParam flightParam = new FlightParam();
            flightParam.setFlightNo(flySegment.getFlightNo());
            flightParam.setFlightDate(sdf.format(flySegment.getDepTime()));

            tabMembIntegral.setOrderNo(String.valueOf(data.getFlyOrder().getId()));
            tabMembIntegral.setMoney(data.getFlyOrder().getTotalPrice());
            tabMembIntegral.setFlightDate(flySegment.getDepTime());
            tabMembIntegral.setAirlineCode(flySegment.getFlightNo().substring(0,2));
            List<FlyTicketVo> flyTicketVos = (List<FlyTicketVo>) ticketService.selectVoByPrimaryKey(data.getFlyOrder().getId()).getData();
            for (FlyTicketVo ticketVo:flyTicketVos) {
                FlyTickets flyTickets = ticketVo.getFlyTickets();
                if(flySegment.getDepCode().equals(flyTickets.getOrgCity()) && flySegment.getArrCode().equals(flyTickets.getDestCity())){
                    tabMembIntegral.setTicketNo(ticketVo.getTicketNo());
                }
            }
            //iss_code?
            tabMembIntegral.setOrderId(data.getFlyOrder().getOrderId());
            tabMembIntegral.setPassengerSegmentId(flySegment.getId() + "");
            //transaction_code?交易编码
            //product_code?产品代码
            tabMembIntegral.setFlightNum(flySegment.getFlightNo());
            tabMembIntegral.setCarbineClass(flySegment.getCabinClass());
            tabMembIntegral.setStartCode(flySegment.getDepCode());
            tabMembIntegral.setArriveCode(flySegment.getArrCode());
            tabMembIntegral.setDisposeTime(new Date());

            TabIntegralRecord tabIntegralRecord = new TabIntegralRecord();
            tabIntegralRecord.setOrderId(data.getFlyOrder().getOrderId());
            tabIntegralRecord.setCreateTime(new Date());

            String flyDistance = HttpClientUtil.doPostJson("http://47.75.92.162:49001/av/flyDistance", JSON.toJSONString(flightParam));
            Double integral = 0.0;
            if(StringUtils.isBlank(flyDistance)){
                flyDistance = flySegment.getDistance();
            }else{
                Double distance = Double.valueOf(flyDistance);
                if(distance.doubleValue() <= 0) flyDistance = flySegment.getDistance();
            }
            integral = Double.valueOf(flyDistance);
            tabMembIntegral.setIntegralNum(integral);
            tabIntegralRecord.setAddIntegral(integral);
            tabIntegralRecord.setModifyReason("BOOKING");
            tabIntegralRecord.setStatus("NORMAL");
            tabMembIntegralService.addMembIntegral(tabMembIntegral,tabIntegralRecord);
        }*/
    }

    public void refundIntegral(String orderId){
        List<TabMembIntegral> tabMembIntegrals = tabMembIntegralService.getIntegralInfo(orderId);
        Date now = new Date();
        for (TabMembIntegral tabMembIntegral:tabMembIntegrals) {
            double integral = tabMembIntegral.getIntegralNum();
            tabMembIntegral.setIntegralNum(0d);
            tabMembIntegral.setIntegralStatus(IntegralStatus.INVALID.getStatus());
            tabMembIntegral.setDisposeTime(now);
            TabIntegralRecord tabIntegralRecord = new TabIntegralRecord();
            tabIntegralRecord.setIntegralId(tabMembIntegral.getIntegralId());
            tabIntegralRecord.setOrderId(tabMembIntegral.getOrderId());
            tabIntegralRecord.setCreateTime(now);
            tabIntegralRecord.setAddIntegral(0d);
            tabIntegralRecord.setReduceIntegral(integral);
            tabIntegralRecord.setModifyReason("REFUND");
            tabIntegralRecord.setStatus("INVALID");
            tabMembIntegralService.delMembIntegral(tabMembIntegral,tabIntegralRecord);
        }
    }
}
